using System;
using System.Xml;
using System.Globalization;

namespace JiveMessengerLinux
{
	public class LogElement : IComparable<LogElement>
	{
		public int SessionID { get; set; }
		public DateTime TimeStamp { get; private set; }
		public string From { get; private set; }
		public string To { get; private set; }
		public string Text { get; private set; }
		private string Font { get; set; }
		private string FontStyle { get; set; }
		private string FontWeight { get; set; }
		private string ColorHex { get; set; }
		
		private string StyleString
		{
			get
			{
				string style = "font-family:" + this.Font + "; ";
				if (!String.IsNullOrEmpty(this.FontWeight))
					style += "font-weight:" + this.FontWeight + "; ";
				if (!String.IsNullOrEmpty(this.FontStyle))
					style += "font-style:" + this.FontStyle + "; ";
				
				style += "color:#" + this.ColorHex + "; ";
				
				return style;
				
			}
			set
			{
				string[] styles = value.Split(';');
				foreach (string str in styles)
				{
					if (str.Contains("font-family"))
						this.Font = str.Replace("font-family:", "").Replace(";", "").Trim();
					else if (str.Contains("font-weight"))
						this.FontWeight = str.Replace("font-weight:", "").Replace(";", "").Trim();
					else if (str.Contains("font-style"))
						this.FontStyle = str.Replace("font-style:", "").Replace(";", "").Trim();
					else if (str.Contains("color"))
					{
						string hexvalue = str.Replace("color:", "").Replace(";", "").Replace("#","").Trim();
						this.ColorHex = hexvalue;
					}
				}
				
			}
		}
		
		public int CompareTo(LogElement other)
		{
			return DateTime.Compare(this.TimeStamp, other.TimeStamp);//String.Compare(this.ShowName, other.ShowName, StringComparison.Ordinal);
		}
		
		public LogElement(DateTime timeStamp, int sessionID, string from, string to, string text, string font, string fontStyle, string colorHex)
		{
			this.TimeStamp = timeStamp;
			this.SessionID = sessionID;
			this.From = from;
			this.To = to;
			this.Text = text;
			//Text = Helpers.EregiReplace(TextMethods.HyperlinkRegex, "xlink:href='\\2\\4\\5' xlink:show='new'>", text);
			this.Font = font;
			// FontStyle = fontStyle;
			// FontWeight = fontWeight;
			//SolidColorBrush col = (SolidColorBrush)new BrushConverter().ConvertFromString(color);
			//this.ColorHex = Helpers.ColorToHexString(col.Color);
			this.ColorHex = colorHex;
            this.FontWeight = Client.ParseFontWeigth(fontStyle).ToString();
            this.FontStyle = Client.ParseFontStyle(fontStyle).ToString();
//			switch (fontStyle)
//			{
//                case "Light":
//                    this.FontWeight = "Light";//FontWeights.Light.ToString();
//                    this.FontStyle = "Normal";// FontStyles.Normal.ToString();
//				break;
//                case "Regular":
//                    this.FontWeight = "Regular";// FontWeights.Regular.ToString();
//                    this.FontStyle = "Normal";//FontStyles.Normal.ToString();
//				break;
//                case "Italic":
//                    this.FontStyle = "Italic";// FontStyles.Italic.ToString();
//                    this.FontWeight = "Normal";//FontWeights.Normal.ToString();
//				break;
//                case "Semibold":
//                    this.FontWeight = "Semibold";// FontWeights.SemiBold.ToString();
//                    this.FontStyle ="Normal";// FontStyles.Normal.ToString();
//				break;
//                case "Bold":
//                    this.FontWeight = "Bold";// FontWeights.Bold.ToString();
//                    this.FontStyle = "Normal";//FontStyles.Normal.ToString();
//				break;
//			case "Bold Italic":
//                    this.FontWeight = "Bold";//FontWeights.Bold.ToString();
//                    this.FontStyle = "Italic";// FontStyles.Italic.ToString();
//				break;
//                case "Light Oblique":
//                    this.FontWeight = "Light";//FontWeights.Light.ToString();
//                    this.FontStyle = "Oblique";// FontStyles.Oblique.ToString();
//				break;
//			case "Semibold Oblique":
//                    this.FontWeight ="Semibold";// FontWeights.SemiBold.ToString();
//                    this.FontStyle = "Oblique";// FontStyles.Oblique.ToString();
//				break;
//				
//			}
		}
		
		public bool IsOK()
		{
			return this.SessionID != -1 && !String.IsNullOrEmpty(this.Text) && !String.IsNullOrEmpty(this.To) &&!String.IsNullOrEmpty(this.From) && this.TimeStamp!= DateTime.MinValue;
		}
		
		public LogElement(XmlReader r)
		{
			this.SessionID = -1;
			r.Read();
			if (r.Name != "Message")
				return;
			DateTime timeStamp;
			if (DateTime.TryParse(r.GetAttribute("DateTime"), CultureInfo.CurrentCulture, DateTimeStyles.None, out timeStamp))
				this.TimeStamp = timeStamp;
			int id;
			if (!Int32.TryParse(r.GetAttribute("SessionID"), out id))
				return;
			this.SessionID = id;
			r.Read();
			while (!r.EOF)
			{
				if (r.Name.Equals("From"))
				{
					XmlReader r2 = r.ReadSubtree();
					r2.Read();
					while (!r2.EOF)
					{
						
						if (r2.Name == "From" && !r2.IsStartElement())
							break;
						if (r2.Name == "User")
							this.From = r2.GetAttribute("FriendlyName");
						r2.Read();
					}
					r.Read();
				}
				else if (r.Name.Equals("To"))
				{
					XmlReader r2 = r.ReadSubtree();
					r2.Read();
					while (!r2.EOF)
					{
						
						if (r2.Name == "To" && !r2.IsStartElement())
							break;
						if (r2.Name == "User")
							this.To = r2.GetAttribute("FriendlyName");
						r2.Read();
					}
					r.Read();
				}
				else if (r.Name == "Text")
				{
					string txt = r.GetAttribute("Style");
					this.Text = r.ReadElementContentAsString();
					
					this.StyleString = txt;
				}
				else
					if ((r.IsEmptyElement) || !r.IsStartElement())
						r.ReadOuterXml();
				else
				{
					r.Read();
				}
				
				
				//  if ((r.Name.Equals("Message")) && (!r.IsStartElement()))
				//    break;
				// r.Read();
			}
		}
		
		public void WriteXml(XmlWriter w)
		{
			
			
			w.WriteStartElement("Message");
			
			w.WriteAttributeString("Date", this.TimeStamp.ToShortDateString());
			w.WriteAttributeString("Time", this.TimeStamp.ToString("HH:mm:ss"));
			w.WriteAttributeString("DateTime", this.TimeStamp.ToString(CultureInfo.CurrentCulture));
			w.WriteAttributeString("SessionID", this.SessionID.ToString(CultureInfo.InvariantCulture));
			
			w.WriteStartElement("From");
			w.WriteStartElement("User");
			w.WriteAttributeString("FriendlyName", this.From);
			w.WriteEndElement();//User
			w.WriteEndElement();//From
			
			w.WriteStartElement("To");
			w.WriteStartElement("User");
			w.WriteAttributeString("FriendlyName", this.To);
			w.WriteEndElement();//User
			w.WriteEndElement();//To
			
			
			w.WriteStartElement("Text");
			w.WriteAttributeString("Style", this.StyleString);
			w.WriteValue(this.Text);
			w.WriteEndElement();//Text
			w.WriteEndElement();//Message
			
		}
		
		
	}
}

